function [list, err] = pastenote(proto, notefile, dfile, filename)

% paste a note into the window selected by notefile as text
% assumes proto is a structure as in protocol as shown here:
% protocol -> protocol is an array structure itself, as follows:
%		protocol(n).proto	the name of the protocol
%		protocol(n).seq		the sequence that was to be run
%		protocol(n).frec		the first record in the protocol
%		protocol(n).lrec		the last record in the protocol
%		protocol(n).ztime		the starting time of the protocol in ztime ( set to 0 here; can be set later when data file is read)
%		protocol(n).etime		the starting time of the protocol in "real time" (read from notefile)
%		protocol(n).valve		the valve in use at the time.
%		protocol(n).notes		the concatenation of note entries since the last protocol

err=0;
np=size(proto);
list='';
h1=0;
if(~strcmp(notefile, 'main'))
   h1=findobj('Tag', notefile);
   if(~ishandle(h1)) return
   end
end
set(h1, 'Value', 1);
if(strmatch(computer, 'MAC')) % font stuff...
    set(h1,'FontName', 'FixedWidth');
    set(h1, 'FontSize', 10);
    set(h1, 'FontWeight', 'bold');
end;

if(~isempty(dfile)) 
   list = sprintf('File: %s  Recs: %5d   Mode: %2d  Points: %5d', dfile.filename, dfile.records_in_file, dfile.mode, dfile.nr_points);
   s = size(dfile.comment);
   c = char(dfile.comment);
   for i = 1:s(1)
      list = strvcat(list, sprintf('Comment: %s', dfile.comment(i,:)));
   end;
else
   list = sprintf('NoteFile: %s', filename);
end


valve=1;
for i=1:np(2)
%   if(size(proto(i).notes) > 1)
%      for j=1:size(proto(i).notes)
%         list = strvcat(list, sprintf('  %s',proto(i).notes(j).note));
%      end
%   end
	if(~isempty(proto(i).notes))
		al=strcat('* ', char(proto(i).notes));
		list=strvcat(list, al);
	end;
  % check the valve situation
   if(~isempty(proto(i).valve) & proto(i).valve ~= valve) % display it if it has changed.
   		valve=proto(i).valve;
		list = strvcat(list, sprintf('@ VALVE: %3d ', valve));
	end;
	   
   % now put up the protocol;
   if(proto(i).lrec >= proto(i).frec)
	   cmd=sprintf('> %4d %4d %8s %14s ', proto(i).frec, proto(i).lrec, proto(i).proto, proto(i).seq);
   else
      cmd='';
   end
   list = strvcat(list, cmd);
   
end
if(strcmp(notefile, 'main'))
   disp(list);
else
   NN=length(cellstr(list));
   if(nargout == 0) % no output arguments, then we display in the window.
   	set(h1, 'String', list);
        nmin = get(h1, 'Min'); % make sure we allow multple selectoin
	   nmax = get(h1, 'Max');
   	if((nmax-nmin) < NN)
	      set(h1,'Min', 1);
   	   set(h1, 'Max', NN-1);
	   end;
	end; % otherwise, just return the list
end
return;
